#include "serialmanager.h"

SerialManager::SerialManager(QObject *parent) : QObject(parent) {
    connect(&serial, &QSerialPort::readyRead, this, &SerialManager::onReadyRead);
}

SerialManager::~SerialManager() {
    close();
}

void SerialManager::open(const QString &portName, qint32 baudRate) {
    serial.setPortName(portName);
    serial.setBaudRate(baudRate);
    serial.open(QIODevice::ReadWrite);
}

void SerialManager::close() {
    if (serial.isOpen())
        serial.close();
}

void SerialManager::send(const QByteArray &data) {
    if (serial.isOpen())
        serial.write(data);
}

void SerialManager::onReadyRead() {
    emit received(serial.readAll());
}